// pages/my/logon/findPassword/findPassword.js
const LApp = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    changeInfo: {},
    verificationCode: "",
    forbidden: false,
    buttonText: '获取验证码',
    Type: 'primary'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options) {
      this.data.changeInfo.loginName = options.userName;
      this.setData({ changeInfo: this.data.changeInfo });
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  BindInput: function (e) {
    wx.$$apply(e, this.data.changeInfo);
  },
  Submit: function () {

    var info = this.data.changeInfo;
    if (!info.loginName) {
      wx.showModal({
        title: '提示',
        content: '请输入登录名',
      })
    }
    if (!info.phoneNumber) {
      wx.showModal({
        title: '提示',
        content: '请输入手机号',
      })
    }
    if (!info.pwd) {
      wx.showModal({
        title: '提示',
        content: '请输入密码',
      })
    }
    if (!info.verificationCode) {
      wx.showModal({
        title: '提示',
        content: '请输入验证码',
      })
    }
    if (info.verificationCode != this.data.verificationCode) {
      wx.showModal({
        title: '提示',
        content: '验证码不正确',
      })
    }
    
    LApp._post({
      url: 'nbapi/my/forgetpwd',
      data: info,
      success: function (res) {
        if (res) {
          wx.showModal({
            content: '密码修改成功',
          })
        }
      },
      fail: function (ex) {
        if (ex) {
          wx.showModal({
            title: '提示',
            content: ex,
          })
        }
      }
    })
  },
  GetVerificationCode: function (e) {
    let _this = this;
    var phoneNumber = this.data.changeInfo.phoneNumber;

    if (!this.validatemobile(phoneNumber)) return;
    var name = this.data.changeInfo.loginName;
    if (!name) {
      wx.showModal({
        title: '提示',
        content: '请填写登录名',
      })
    }
    _this.time();
    LApp._get({
      url: 'nbapi/auth/getverificationcode',
      data: { phoneNumber: phoneNumber, name: name },
      success: function (res) {
        if (res) {
          _this.setData({
            verificationCode: res
          })
        }
      },
      fail: function (ex) {
        wx.showModal({
          title: '提示',
          content: ex,
        })
      }
    })
  },
  //验证手机号
  validatemobile: function (mobile) {
    if (mobile.length == 0) {
      wx.showToast({
        title: '请输入手机号！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    if (mobile.length != 11) {
      wx.showToast({
        title: '手机号长度有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1})|(17[0-9]{1}))+\d{8})$/;
    if (!myreg.test(mobile)) {
      wx.showToast({
        title: '手机号有误！',
        icon: 'success',
        duration: 1500
      })
      return false;
    }
    return true;
  },
  time: function () {
    var t = 60;

    var that = this;
    function TimeCount() {
      t--;
      if (t <= 0) {
        that.setData({
          forbidden: false,
          buttonText: '获取验证码',
          Type: 'primary'
        })
      }
      else {
        that.setData({
          forbidden: true,
          buttonText: '重新发送（' + t + '）',
          Type: 'default'
        })
        setTimeout(TimeCount, 1000);
      }

    }
    setTimeout(TimeCount, 100);


  },


})